package com.it.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.OrderService;
import com.it.dao.OrderDetailMapper;
import com.it.dao.OrderMapper;
import com.it.dao.ProductMapper;
import com.it.dao.ProductSkuMapper;
import com.it.pojo.Order;
import com.it.pojo.OrderDetail;
import com.it.pojo.Product;
import com.it.pojo.ProductSku;
import com.it.pojo.params.order.OrderInfoQuery;
import com.it.pojo.params.order.OrderQuery;
import com.it.pojo.vo.order.OrderInfoVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderDetailMapper orderDetailMapper;


    @Resource
    private ProductSkuMapper productSkuMapper;


    @Resource
    private ProductMapper productMapper;

    @Override
    public Map<String, String> saveOrder(OrderQuery orderQuery) {
        Integer num = orderQuery.getNum();
        Integer productId = orderQuery.getProductId();
        Integer userId = orderQuery.getUserId();
        Integer price = orderQuery.getPrice();
        Order order = new Order();
        OrderDetail orderDetail = new OrderDetail();
        Date date = new Date(); // 当前时间
        order.setUserId(userId);
        order.setGmtCreate(date);
        order.setStatus(0); // 未付款
        order.setSendStatus(0); // 发货状态
        long mostSignificantBits = Long.parseLong(((UUID.randomUUID().getMostSignificantBits() * -1) + "").substring(0, 10));
        System.out.println("mostSignificantBits = " + mostSignificantBits);
        order.setOrderId(mostSignificantBits);
        int insert = orderMapper.insert(order);

        Product product = productMapper.selectById(productId);

        LambdaQueryWrapper<ProductSku> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ProductSku::getProductId, productId);
        ProductSku productSku = productSkuMapper.selectOne(wrapper);
        if (product != null && productSku != null) {
            orderDetail.setProductName(product.getProductName());
            orderDetail.setProductImg(productSku.getSkuImg());
            orderDetail.setSkuId(productSku.getSkuId());
            orderDetail.setProductPrice(productSku.getSellPrice());

        }
        orderDetail.setGmtModified(date);
        orderDetail.setTotalAmount(num * price); // 单价*数量
        orderDetail.setProductPrice(price); // 单价
        orderDetail.setOrderId(mostSignificantBits);
        orderDetail.setGmtCreate(date);
        orderDetail.setBuyTime(date);
        orderDetail.setNum(num);
        orderDetail.setProductId(productId);
        int insert1 = orderDetailMapper.insert(orderDetail);
        Map<String, String> map = new HashMap<>();
        map.put("orderId", mostSignificantBits + "");
        return map;
    }

    @Override
    public int updateOrderStatus(Long orderId) {
        Order order = new Order();
        Date date = new Date();
        order.setGmtModified(date);
        order.setStatus(1);
        order.setOrderId(orderId);
        int i = orderMapper.updateById(order);
        return i;
    }

    @Override
    public int getOrderStatus(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        if (order != null) {
            return order.getStatus() == 1 ? 1 : 0;
        }
        return 0;
    }

    @Override
    public Map<String, Object> listOrder(OrderInfoQuery orderInfoQuery) {
        Integer limit = orderInfoQuery.getLimit();
        Integer start = (orderInfoQuery.getPageNum() - 1) * limit;
        Integer status = orderInfoQuery.getStatus();
        Integer sendStatus = orderInfoQuery.getSendStatus();

        String startTime = null;
        Integer userId = orderInfoQuery.getUserId();
        String endTime = null;
        Map<String, Object> map = new HashMap<>();
        if (orderInfoQuery.getDate().length != 0) {
            startTime = orderInfoQuery.getDate()[0].substring(0, 10);
            endTime = orderInfoQuery.getDate()[1].substring(0, 10);
        }
        List<OrderInfoVO> orderInfoVOList = orderMapper.listOrderInfo(start, limit, startTime, endTime, userId,status,sendStatus);
        Integer total = orderMapper.listOrderInfoTotal( startTime, endTime, userId,status,sendStatus);
        map.put("orderList", orderInfoVOList);
        map.put("total", total);

        return map;
    }
}
